#include <iostream>
#include <assert.h>
#include <stdbool.h>
#include <algorithm>
using namespace std;

namespace wanghao
{
    //模板类
    template<class T>
    class vector
    {
    public:
        //普通迭代器
        typedef T* iterator;
        //const迭代器
        using const_iterator = const T*;
        //无参构造,全部给空即可
        vector() {}
        //initializer_list构造
        vector(initializer_list<T> il)
        {
            reserve(il.size());
            for(auto& e : il)
            {
                push_back(e);
            }
        }
        //拷贝构造
        vector(const vector<T>& v)
        {
            reserve(v.capacity());
            for(auto& e : v)
            {
                push_back(e);
            }
        }
        //两个对象的交换
        void swap(vector<T>& v)
        {
            std::swap(_start,v._start);
            std::swap(_finish,v._finish);
            std::swap(_endofstorage,v._endofstorage);
        }
        //赋值构造
        vector<T>& operator=(vector<T> v)
        {
            swap(v);
            return *this;
        }
        //迭代器区间构造
        template<class InputIterator>
        vector(InputIterator first,InputIterator last)
        {
            while(first != last)
            {
                push_back(*first);
                first++;
            }
        }
        //n个val构造
        vector(size_t n,const T& val = T())
        {
            reserve(n);
            for(size_t i = 0;i < n;i++)
            {
                push_back(val);
            }
        }
        vector(int n,const T& val = T())
        {
            reserve(n);
            for(int i = 0;i < n;i++)
            {
                push_back(val);
            }
        }
        //析构函数
        ~vector()
        {
            if(_start)
            {
                delete[] _start;
                _start = _finish = _endofstorage;
            }
        }
        //resize
        void resize(size_t n,T value = T())
        {
            //如果n小于size()的话就是删除数据
            if(n <= size())
            {
                _finish = _start + n;
            }
            else
            {
                reserve(n);
                while(_finish < _start + n)
                {
                    *_finish = value;
                    _finish++;
                }
            }
        }
        //扩容reserve
        void reserve(size_t n)
        {
            //判断n是否合理,因为不存在缩容
            if(n > capacity())
            {
                int oldSize = size();
                //new空间,这里尽量用new,不要用malloc,因为用malloc的话后面要用定位new
                T* tmp = new T[n];
                if(_start)
                {
                    //拷贝旧数据
                    for(int i = 0;i < oldSize;i++)
                    {
                        *(tmp + i) = *(_start + i);
                    }
                    //释放旧空间
                    delete[] _start;
                }
                //更新_start
                _start = tmp;
                //更新_finish以及_endofstorage
                _finish = _start + oldSize;
                _endofstorage = _start + n;
            }
        }
        //下标+[]遍历
        T& operator[](size_t n)
        {
            //判断n是否合理
            assert(n < size());
            return *(_start + n);
        }
        //下标+[]遍历
        const T& operator[](size_t n) const
        {
            //判断n是否合理
            assert(n < size());
            return *(_start + n);
        }
        //size
        size_t size() const
        {
            return _finish - _start;
        }
        //capacity
        size_t capacity() const
        {
            return _endofstorage - _start;
        }
        //push_back
        void push_back(const T& x)
        {
            //首先判断空间是否满了
            // if(_finish == _endofstorage)
            // {
            //     //调用reserve申请空间
            //     reserve(capacity() == 0 ? 4 : 2 * capacity());
            // }
            // *_finish = x;
            // _finish++;

            //复用insert
            insert(_finish,x);
        }
        //普通迭代器
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        //const迭代器
        const_iterator begin() const
        {
            return _start;
        }
        const_iterator end() const
        {
            return _finish;
        }
        //判断容器是否为空
        bool empty()
        {
            return _start == _finish;
        }
        //pop_back
        void pop_back()
        {
            //断言,容器为空就不能进行删除
            assert(!empty());
            --_finish;
        }
        //指定位置插入数据
        iterator insert(iterator pos,const T& x)
        {
            //判断pos位置是否合理
            assert(pos >= _start && pos <= _finish);
            //这里会存在pos失效的问题,所以我们要先记录pos和_start的距离
            int oldn = pos - _start;
            //判断空间是否足够
            if(_endofstorage == _finish)
            {
                reserve(capacity() == 0 ? 4 : 2 * capacity());
            }
            //挪动数据
            pos = _start + oldn;
            iterator i = _finish - 1;
            while(i >= pos)
            {
                *(i + 1) = *i;
                i--;
            }
            *pos = x;
            ++_finish;
            return pos;
        }
        //指定位置删除数据
        iterator erase(iterator pos)
        {
            //判断pos位置是否合理
            assert(pos >= _start && pos < _finish);
            iterator i = pos + 1;
            while(i < _finish)
            {
                *(i - 1) = *i;
                i++;
            }
            --_finish;
            return pos;
        }

    private:
        iterator _start = nullptr;
        iterator _finish = nullptr;
        iterator _endofstorage = nullptr;
    };
    void _vector_test1()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        v.push_back(5);
        //打印
        for(int i = 0;i < v.size();i++)
        {
            cout << v[i] << " ";
        }
        cout << endl;
        //迭代器遍历
        vector<int>::iterator it = v.begin();
        while(it != v.end())
        {
            cout << *it << " ";
            it++;
        }
        cout << endl;
        //指定位置插入
        v.pop_back();
        v.pop_back();
        v.pop_back();
        //范围for遍历
        for(const auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
    }
    void _vector_test2()
    {
        const vector<int> v;
        vector<int>::const_iterator it = v.begin();
        while(it != v.end())
        {
            cout << *it << " ";
            it++; 
        }
        cout << endl;
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
    }
    void _vector_test3()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        //v.push_back(5);
        //v.push_back(6);
        //cout << "size:" << v.size() << "|" << "capacity:" << v.capacity() << endl;;
        //遍历
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
        //指定位置插入
        v.insert(v.begin()+2,30);
        //cout << "size:" << v.size() << "|" << "capacity:" << v.capacity() << endl;;
        //迭代器遍历
        vector<int>::iterator it = v.begin();
        while(it != v.end())
        {
            cout << *it << " ";
            it++;
        }
        cout << endl;
    }
    void _vector_test4()
    {
        vector<int> v({1,2,3,4,5,6,7,8,9,10});
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
        //删除
        v.erase(v.begin() + 9);
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
    }
    //insert迭代器失效
    void _vector_test5()
    {
        vector<int> v({1,2,3,4});
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
        int index = 0;
        cin >> index;
        //查找
        vector<int>::iterator it = find(v.begin(),v.end(),index);
        if(it != v.end())
        {
            //it是否失效 - 已经失效
            it = v.insert(it,10 * index);
            cout << *it << endl;
        }
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
    }
    //erase迭代器失效
    void _vector_test6()
    {
        vector<int> v({1,2,3,4});
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
        int index = 0;
        cin >> index;
        //查找
        vector<int>::iterator it = find(v.begin(),v.end(),index);
        if(it != v.end())
        {
            //it是否失效 - 已经失效
            v.erase(it);
            cout << *it << endl;
        }
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
    }
    //erase迭代器失效 - 要求删除所有的偶数
    void _vector_test7()
    {
        vector<int> v({1,2,2,3,4});
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
        vector<int>::iterator it = v.begin();
        while(it != v.end())
        {
            if(*it % 2 == 0)
            {
                //失效的迭代器不能访问,更新之后才能访问
                it = v.erase(it);
            }
            else
            {
                it++;
            }
        }
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
    }
    //resize
    void _vector_test8()
    {
        vector<int> v({1,2,2,3,4});
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
        v.resize(10);
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
        v.resize(20,11);
        for(auto& e : v)
        {
            cout << e << " ";
        }
        cout << endl;
    }
    //拷贝构造
    void _vector_test9()
    {
        vector<int> v1({1,2,3,4,5,6,7,8,9,10});
        vector<int> v2({1,1,1,1,1,1,1,1,1,1,1});
        v2 = v1;
        for(auto& e : v2)
        {
            cout << e << " ";
        }
        cout << endl;
    }
    //迭代器区间构造
    void _vector_test10()
    {
        vector<int> v1(10,9);
        vector<int> v2(v1.begin(),v1.end());
        for(auto& e : v2)
        {
            cout << e << " ";
        }
        cout << endl;
    }
    void _vector_test11()
    {
        vector<string> v1;
        v1.push_back("1111111111111111111111111");
        v1.push_back("1111111111111111111111111");
        v1.push_back("1111111111111111111111111");
        v1.push_back("1111111111111111111111111");
        v1.push_back("1111111111111111111111111");
        for(auto& e : v1)
        {
            cout << e << " ";
        }
        cout << endl;
    }
}


